package com.smart.business.order.service;

import com.msart.api.dto.order.CartDto;
import com.msart.api.dto.order.OrderDto;
import com.msart.api.dto.store.StoreDto;
import com.msart.api.service.OrderService;
import com.msart.api.service.StorageService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 加入购车 ---> 结算 ---> 确认购买 ---> 生成订单 --- >  确认支付(支付方式)- 后台支付的接口
 * 商品的ID
 * 商品的数量
 */
@Service
public class BusinessOrderService {
    @Reference(check = false)
    OrderService orderService;
    @Reference(check = false)
    StorageService storageService;

    /**
     * 订单号   总价  商品描述
     * @param order
     */
    public OrderDto createOrder(OrderDto order) {
        // 保存订单信息
        OrderDto order1 = orderService.createOrder(order);
        // 减少库存
        List<CartDto> carts = order.getCarts();
        List<StoreDto> stores = new ArrayList<>();
        carts.forEach((cart) -> {
            StoreDto store = new StoreDto();
            store.setProductId(cart.getProductId());
            store.setNum(cart.getNum());
            stores.add(store);
        });
        storageService.decrease(stores);
        return  order1;
    }

    public BigDecimal total(){
        return null;
    }



}
